package com.xishu.service.order.callback;

import com.xishu.entity.customer.Customer;
import com.xishu.entity.order.Order;
import com.xishu.entity.shop.RechargeItem;
import com.xishu.service.CommonService;
import com.xishu.service.CustomerService;
import com.xishu.service.LockService;
import com.xishu.service.order.base.OrderService;
import com.xishu.util.Tools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static com.xishu.config.Constant.ORDER_STATUS_END;

public class OrderCallBackService implements Tools {
    private static Logger logger = LoggerFactory.getLogger(OrderCallBackService.class);
    private static OrderCallBackService instance = new OrderCallBackService();
    private CommonService commonService = CommonService.getInstance();

    private LockService localLockService = LockService.getInstance();

    private OrderService orderService = OrderService.getInstance();

    private OrderCallBackService() {
    }

    public static OrderCallBackService getInstance() {
        return instance;
    }

    public void orderCallback(Order order) throws Exception {
        OrderCallBackChain orderCallBackChain = new OrderCallBackChain();
        orderCallBackChain.callback(order);
    }

    /**
     * 保留
     *
     * 处理VIP订单
     *
     * @param order
     */
    public void dealVipOrder(Order order) throws Exception {
        if (getInt(order.getStatus()) == ORDER_STATUS_END) {
            logger.info("order status is end, shopId is {}, orderId is {}", order.getShopId(), order.getId());
            return;
        }

        Long customerId = order.getCustomerId();
        Customer customer = CustomerService.getInstance().findCustomer(customerId);
        customer.setVip(true);
        customer.setVipEndTime(order.getVipEndTime());
        commonService.save(customer);

        //修改订单状态
        order.setStatus(ORDER_STATUS_END);
        order.setPayed(true);
        commonService.save(order);

        //给VIP用户添加优惠券
        CustomerService.getInstance().addLevelTicketToCustomer(customer);
    }

    /**
     * 处理用户充值
     * @param order
     * @throws Exception
     */
    public void dealCustomerRecharge(Order order) throws Exception {
        String lockKey = orderService.getLockKey(order.getShopId(), order.getId());
        Object lock = localLockService.getLock(lockKey);
        synchronized (lock) {
            if (getInt(order.getStatus()) == ORDER_STATUS_END) {
            logger.info("recharge_order status is end shopId is {},orderId is {}", order.getShopId(), order.getId());
            return;
        }
            Long customerId = order.getCustomerId();
            Customer customer = CustomerService.getInstance().findCustomer(customerId);
            Long rechargeItemId = order.getRechargeItemId();
            RechargeItem rechargeItem = commonService.findEntity(rechargeItemId, RechargeItem.class);
            //给账户添加充值余额
            customer.setAccount(rechargeItem.getGiveMoney());
            commonService.save(customer);
            //修改订单状态
            order.setStatus(ORDER_STATUS_END);
            order.setPayed(true);
            commonService.save(order);
        }
    }
}
